package com.itheima.filter;

import com.itheima.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/loginServlet")
public class FilterDemo02_login implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
//获取session中的内容
        User user = (User) request.getSession().getAttribute("u");
        //判断u中是否有内容
        if ("u"==null){
            //没有返回登陆页面
            response.sendRedirect("/login.html");
        }else {
            //有放行
            chain.doFilter(request, response);
        }

    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}